package com.xiechuang.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 随机数助手
 * @Author: xiechuang
 * @Date: 2023/1/11
 */
public class RandomHelper {

    /**
     * 根据list里的内容随机获取 number个
     *
     * @param contents
     * @param returnSize
     * @return
     */
    public static List get(List contents, int returnSize) {
        if (returnSize < contents.size()) {
            if (returnSize < contents.size() / 2) {
                List randomList = new ArrayList();
                int index;
                for (int i = 0; i < returnSize; i++) {
                    index = (int) (Math.random() * (contents.size() - 1));
                    randomList.add(contents.get(index));
                    contents.remove(index);
                }
                return randomList;
            } else {
                while (contents.size() > returnSize) {
                    int index = (int) (Math.random() * (contents.size() - 1));
                    contents.remove(index);
                }
                return contents;
            }
        }
        return contents;
    }

    /**
     * 32位UUID
     *
     * @return
     */
    public static String getUUID(int length) {
        String id = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        if (length == 16) {
            return id.substring(8, 24);
        } else {
            return id.substring(0, length);
        }
    }

    public static String getUUID() {
        return getUUID(32);
    }

    /**
     * 获得随即生成的字符串
     *
     * @param value
     * @return
     */
    public static String randomByScope(int value) {
        if (value == 0) {
            return "1";
        }
        value = (int) (Math.random() * value);
        return String.valueOf((value == 0) ? 1 : value);
    }

    public static String randomByLength(int length) {
        String result = new String();
        for (int i = 0; i < length; i++) {
            result += (int) (Math.random() * 10);
        }
        return result;
    }

    public static Integer getInt(int value) {
        if (value == 0) {
            return 1;
        }
        value = (int) (Math.random() * value);
        return value;
    }

    public static Long getLong(long value) {
        if (value == 0) {
            return 1L;
        }
        value = (long) (Math.random() * value);
        return value;
    }

    public static String getString(int value) {
        if (value == 0) {
            return "1";
        }
        value = (int) (Math.random() * value);
        return String.valueOf(value);
    }

}
